package com.league.zjxn.main

import android.content.Context
import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.view.OptionsPickerView
import com.blankj.utilcode.util.ConvertUtils
import com.league.zjxn.R
import com.league.zjxn.jetpack.vmodel.LeagueViewModel
import com.league.zjxn.main.adapter.LeagueMemberAdapter
import com.league.zjxn.main.mine.LeagueNoticeActivity
import com.league.zjxn.main.popup.ImageZoomPopup
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_main_league.*

class MainLeagueFragment : OwnerViewModeFragment<LeagueViewModel>() {

    companion object {
        fun newInstance(): MainLeagueFragment {
            return MainLeagueFragment()
        }
    }

    private val mLeagueMemberAdapter = LeagueMemberAdapter()

    private var mIndustryId: String? = null
    private val mOptionsIndustry: OptionsPickerView<String> by lazy {
        OptionsPickerBuilder(requireContext()) { options1, option2, options3, v ->
            val industryList = viewModel.industryListLiveData.value
                ?.data?.data ?: return@OptionsPickerBuilder
            mIndustryId = industryList[options1].id
            mtvMainLeagueFirm?.text = industryList[options1].name

            viewModel.leagueEnterprise(
                industry_id = mIndustryId,
                title = metMainLeagueSearchContent?.text.toString()
            )
        }.setLayoutRes(R.layout.popup_picker_options) {
            it.findViewById<TextView>(R.id.tv_submit).setOnClickListener {
                mOptionsIndustry.returnData()
                mOptionsIndustry.dismiss()
            }
            it.findViewById<TextView>(R.id.tv_reset).setOnClickListener {
                mOptionsIndustry.dismiss()
                mIndustryId = null
                mtvMainLeagueFirm?.text = "全部企业"
                viewModel.leagueEnterprise(
                    industry_id = mIndustryId,
                    title = metMainLeagueSearchContent?.text.toString()
                )
            }
            it.findViewById<TextView>(R.id.tv_cancel).setOnClickListener {
                mOptionsIndustry.dismiss()
            }
        }.build()
    }

    private val mImageZoomPopup: ImageZoomPopup by lazy {
        ImageZoomPopup(requireContext())
    }

    override fun viewModel(): LeagueViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_league

    override fun initView() {
        rvMainLeagueBody.apply {
            addItemDecoration(ItemDecoration())
        }.adapter = mLeagueMemberAdapter.apply {
            setHeaderView(createViewHeader(requireContext()))
            val view = View(requireContext()).apply {
                layoutParams = FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    ConvertUtils.dp2px(50f)
                )
            }
            setFooterView(view)
        }
    }

    inner class ItemDecoration : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
        ) {
            val position: Int = parent.getChildAdapterPosition(view)
            if ((position - 1) % 3 == 0) {
                outRect.left = ConvertUtils.dp2px(10f)
            }
            if ((position - 1) % 3 == 2) {
                outRect.right = ConvertUtils.dp2px(10f)
            }
        }
    }

    override fun initData() {
        viewModel.leagueEnterprise()
    }

    override fun initViewMode() {

        viewModel.leagueMemberLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data?.data
                ?: return@observe
            mLeagueMemberAdapter.setNewInstance(data)
        })

        viewModel.industryListLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mOptionsIndustry.setPicker(data.map { it.name })
            mOptionsIndustry.show()
        })
    }

    private var mtvMainLeagueFirm: TextView? = null

    private var metMainLeagueSearchContent: EditText? = null


    private fun createViewHeader(context: Context): View =
        LayoutInflater.from(context).inflate(R.layout.view_main_league_header, (null)).apply {
            mtvMainLeagueFirm = findViewById<TextView>(R.id.tvMainLeagueFirm).apply {
                setOnClickListener {
                    viewModel.userIndustryList()
                }
            }
            metMainLeagueSearchContent = findViewById<EditText>(R.id.etMainLeagueSearchContent)
            findViewById<ImageView>(R.id.ivMainLeagueSearchSubmit).apply {
                setOnClickListener {
                    viewModel.leagueEnterprise(
                        industry_id = mIndustryId,
                        title = metMainLeagueSearchContent?.text.toString()
                    )
                }
            }
            findViewById<ImageView>(R.id.ivMainLeagueNotice).setOnClickListener {
                LeagueNoticeActivity.open()
            }
            findViewById<ImageView>(R.id.ivMainLeagueImage).setOnClickListener {
                mImageZoomPopup.showPopupWindow()
            }
        }
}